﻿<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" Theme="MSN_Morning"  %>

<script runat="server">
    
    Dim name As String = ""
    Dim location1 As locations = New locations()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If IsPostBack Then
            Response.Write("Postback")
        End If
       
        
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString

        Dim sqlConnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        sqlConnection1.ConnectionString = connection_string
        sqlConnection1.Open()
        
        Dim sqlcomand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()

        Dim String_sql As String = "Select * from locations"

        'associa o comando a executar com a conexão
        sqlcomand1.Connection = sqlConnection1

        'definir o sql que o comando vai executar
        sqlcomand1.CommandText = String_sql
        
        GridView1.DataKeyNames = New String() {"id_location"}
        GridView1.DataSource = sqlcomand1.ExecuteReader()
        GridView1.DataBind()
        
        sqlConnection1.Close()
    End Sub 
    
    Public Sub clearfields_Click()
        txt_id_location.Text = ""
        txt_name.Text = ""
        txt_description.Text = ""
        txt_image.Text = ""
        txt_belong_to.Text = ""
    End Sub   

     Public Sub show_location_on_page()
        txt_id_location.Text = location1.id_location
        txt_name.Text = location1.name.ToString()
        txt_description.Text = location1.description.ToString()
        txt_image.Text = location1.id_image
        txt_belong_to.Text = location1.belongs_to
    End Sub

     Protected Sub clear_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        clearfields_Click()
    End Sub
    

    Protected Sub show_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        show_location_on_page()
    End Sub

    Public Sub formfieldtovariable() 'coloca os campos da variavel para o form
        
        location1.id_location = Integer.Parse(txt_id_location.Text)
        location1.name = txt_name.Text
        location1.description = txt_description.Text
        location1.id_image = Integer.Parse(txt_image.Text)
        location1.belongs_to = Integer.Parse(txt_belong_to.Text)
    End Sub

     Protected Sub btn_insert_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
                
        lbl_info.Text = "Success." + location1.insert()
        lbl_info.ForeColor = Drawing.Color.Green
    End Sub

     Protected Sub btn_update_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        
        lbl_info.Text = "Success." + location1.update()
        lbl_info.ForeColor = Drawing.Color.Green
    End Sub

    
    Public Sub variabletoformfield() 'coloca os capos da variavle para
        txt_id_location.Text = location1.id_location.ToString()
        txt_name.Text = location1.name
        txt_description.Text = location1.description
        txt_image.Text = location1.id_image.ToString()
        txt_belong_to.Text = location1.belongs_to.ToString()
    End Sub
    Protected Sub btn_first_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                
        location1.first()
        variabletoformfield()
    
    End Sub

    Protected Sub btn_last_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        location1.last()
        variabletoformfield()
        
    End Sub
    
    
    Protected Sub btn_next_Click(ByVal sender As Object, ByVal e As System.EventArgs)
           
        formfieldtovariable()
        location1.mnext()
        variabletoformfield()
        
    End Sub

    Protected Sub btn_previous_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
       location1.previous()
        variabletoformfield()
    End Sub

    Protected Sub btn_delete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        
        lbl_info.Text = "Success." + location1.delet()
        lbl_info.ForeColor = Drawing.Color.Green
    
    
        'Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        
        location1.id_location = gridview1.selectedvalue.toString()
        location1.select_location_by_id() 'vai aceder a base de dados  
    End Sub

   

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
        Page.ClientScript.RegisterStartupScript("".GetType(), "ClientSript", "alert('rowindex;" + e.RowIndex.ToString() + "');", True)
        

        Page.ClientScript.RegisterStartupScript("".GetType(), "ClientSript", "alert('rowindex;" + e.RowIndex.ToString() + "');", True)        

    End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <h1>
        Locations</h1>

<table>
        <tr>
            <td>
                <asp:Label ID="Label1" runat="server" Text="id_location"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_location" runat="server" Text ="0"> </asp:TextBox>
                 
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label2" runat="server" Text="name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label3" runat="server" Text="description"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_description" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label4" runat="server" Text="id_iamge"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_image" runat="server" text="0"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="Label5" runat="server" Text="belongs_to"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_belong_to" runat="server" text="0"></asp:TextBox>
            </td>
         </tr>
        <tr>
        <td>

                <asp:Button ID="clear" runat="server" Text="Clear" Width="49px" 
                    onclick="clear_Click" style="margin-left: 21px" />
                <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click" Text="Insert" />
                <asp:Button ID="btn_update" runat="server" onclick="btn_update_Click" Text="Update" />
                <asp:Button ID="btn_first" runat="server" Text="<<" onclick="btn_first_Click" />
                <asp:Button ID="btn_previous" runat="server" Text="<" 
                    onclick="btn_previous_Click" />
                <asp:Button ID="btn_next" runat="server" Text=">" onclick="btn_next_Click" />
                <asp:Button ID="btn_last" runat="server" Text=">>" onclick="btn_last_Click" />
                <asp:Button ID="btn_delete" runat="server" Text="Delete" 
                    onclick="btn_delete_Click" />
                <asp:Label ID="lbl_info" runat="server"></asp:Label>
            </td>
        </tr>
        <tr>
        <td>
                &nbsp;</td>
        </tr>
    </table>
    <asp:GridView ID="GridView1" runat="server" 
        onrowdeleting="GridView1_RowDeleting">
        <Columns>
            <asp:CommandField HeaderText="Select" ShowHeader="True" 
                ShowSelectButton="True" />
            <asp:CommandField ButtonType="Button" HeaderText="Delete" 
                ShowDeleteButton="True" ShowHeader="True" />
        </Columns>
     
    </asp:GridView>
</asp:Content>